package com.derbysoft.synxisadapter.dswitch2.translator.impl;

import com.derbysoft.remote.dswitch.core.GenericRQ;
import com.derbysoft.remote.dswitch.core.GenericRS;
import com.derbysoft.remote.dswitch.hotel.dto.HotelAvailRQ;
import com.derbysoft.remote.dswitch.hotel.dto.HotelAvailRS;
import com.derbysoft.remote.dswitch.hotel.dto.HotelAvailRoomStayDTO;
import com.derbysoft.synxisadapter.commons.dto.synxis.ArrayOfRoomStay;
import com.derbysoft.synxisadapter.commons.dto.synxis.OTAHotelAvailRS;
import com.derbysoft.synxisadapter.commons.model.Channel;
import com.derbysoft.synxisadapter.dswitch2.translator.Translator;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;

public class HotelAvailRSTranslatorImpl extends AbstractRQTranslator
        implements Translator<List<OTAHotelAvailRS>, GenericRS<HotelAvailRS>, GenericRQ<HotelAvailRQ>> {

    @Override
    public GenericRS<HotelAvailRS> translate(List<OTAHotelAvailRS> otaHotelAvailRSs, GenericRQ<HotelAvailRQ> param) {
        OTAHotelAvailRS otaHotelAvailRS = otaHotelAvailRSs.get(0);
        return new GenericRS<HotelAvailRS>(null, translatePayload(otaHotelAvailRS, param));
    }

    private HotelAvailRS translatePayload(OTAHotelAvailRS otaHotelAvailRS, GenericRQ<HotelAvailRQ> rq) {
        HotelAvailRS hotelAvailRS = new HotelAvailRS();
        hotelAvailRS.setHotelAvailRoomStay(translateHotelAvailRoomStay(otaHotelAvailRS, rq));
        return hotelAvailRS;
    }

    private HotelAvailRoomStayDTO translateHotelAvailRoomStay(OTAHotelAvailRS otaHotelAvailRS, GenericRQ<HotelAvailRQ> rq) {
        HotelAvailRoomStayDTO hotelAvailRoomStayDTO = new HotelAvailRoomStayDTO();
        hotelAvailRoomStayDTO.setHotelRef(rq.getPayload().getAvailCriteria().getHotelRef());
        ArrayOfRoomStay roomStays = otaHotelAvailRS.getRoomStays();
        if (roomStays != null && CollectionUtils.isNotEmpty(roomStays.getRoomStay())) {
            Channel channel = findChannel(rq.getHeader().getSource().getUniqueID());
            hotelAvailRoomStayDTO.getRoomStays().addAll(createRoomStayDTOs(rq.getPayload().getAvailCriteria().getStayDateRange(),
                    roomStays.getRoomStay().get(0), channel,
                    rq.getPayload().getAvailCriteria().getRoomStayCandidates().getNumberOfUnits()));
        }
        return hotelAvailRoomStayDTO;
    }
}
